package com.quyunshuo.module.home.activity.main

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.data.CoffeeProduct

class CoffeeAdapter(
    private val context: Context,
    private var coffeeList: List<CoffeeProduct>
) : RecyclerView.Adapter<CoffeeAdapter.ViewHolder>() {

    var onItemClickListener: ((Int) -> Unit)? = null

    fun setListener(onItemClickListener: ((Int) -> Unit)?){
        this.onItemClickListener = onItemClickListener
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val albumArt: ImageView = itemView.findViewById(R.id.album_art)
        val title: TextView = itemView.findViewById(R.id.name)

        init {
            itemView.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    onItemClickListener?.invoke(position)
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_music, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SuspiciousIndentation")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val music = coffeeList[position]

        holder.title.text = music.name
        holder.albumArt.setImageResource(R.drawable.setting_icon)
    }

    override fun getItemCount(): Int = coffeeList.size

    fun updateList(newList: List<CoffeeProduct>) {
        coffeeList = newList
        notifyDataSetChanged()
    }


}